﻿/*
 * @author Lee 
 * Time: 20180403 
 * Abstract: SharePoint API的二次封装
 */

using System;
using System.Text.RegularExpressions;

namespace SharePointHelper.DLL.SSOM
{
	public class Validation
	{
        #region 判断URL是否有效
        /// <summary>
        /// 判断URL是否有效
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsValidUrl(string url)
		{
			bool isValid = false;
			try
			{
				if (string.IsNullOrEmpty(url)) { throw new Exception("url passed is null or empty"); }
				if (url.Length < 7 || url.ToLower().Substring(0,7) != "http://") { throw new Exception("Url must start with \"http://\""); }
				isValid = true;
			}
			catch
			{
				isValid = false;
			}
			return isValid;
		}
        #endregion

        #region 判断email address是否有效
        /// <summary>
        /// 判断email address是否有效
        /// 在传递之前一定要修剪电子邮件。
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsValidEmail(string email)
		{
			bool isValid = false;
			if (!string.IsNullOrEmpty(email))
			{
				if (!email.Contains(" "))
				{
					var regex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", RegexOptions.IgnoreCase);
					isValid = regex.IsMatch(email);
				}
			}
			return isValid;
		}
        #endregion
    }
}
